import 'package:hc_ui/config/hc_customer_config.dart';

class HcUrlUtil {
  const HcUrlUtil._();

  /// 图片地址修复
  /// [imageUrl] 图片路径
  static String imageUrlFix({required String imageUrl}) {
    /// 不含http/https 且路径为/开头的为相对路径 补全路径
    /// assets资源开头不含/
    if (!imageUrl.toLowerCase().startsWith("http") &&
        imageUrl.startsWith("/")) {
      String imagePrefix = HcCustomerConfig().imagePrefix;
      imageUrl = imagePrefix + imageUrl;
    }
    return imageUrl;
  }

  ///切割地址获取url和params;
  static Map<String, dynamic> splitUrlParams({required String url}) {
    Map<String, dynamic> result = {};
    ///分离路径和参数
    List<String> splitList = url.split("?");
    //分离参数
    List<String> paramsList = splitList[1].split("&");
    Map<String, dynamic> params = {};
    for (var element in paramsList) {
      //拆分key和value
      List obj = element.split("=");
      params[obj[0]] = obj[1];
    }
    result['url'] = splitList[0];
    result['params'] = params;
    return result;
  }
}
